package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.proof.subview

import android.annotation.SuppressLint
import android.content.res.Resources
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.DatePicker
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.helasasa.maxfunds.lineofcredit.cashloan.R

interface IProofCameroonDatePickerListener {
    fun onClick(date: String, pickTag: String)
}

class ProofCameroonDatePicker(var onItemClick: IProofCameroonDatePickerListener) : DialogFragment() {

    private val TYPE_ONLY_YEAR = 1
    private val TYPE_YEAR_MONTH = 2
    private val TYPE_YEAR_MONTH_DAY = 0

    private lateinit var datePicker: DatePicker
    private lateinit var confirm: TextView

    private var pickType = 0
    var pickTag: String = ""

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return inflater.inflate(R.layout.view_cameroon_proof_date_picker, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        datePicker = view.findViewById(R.id.datePicker)
        confirm = view.findViewById(R.id.confirm)

        initializeView()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.BOTTOM
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        when (pickType) {
            TYPE_ONLY_YEAR -> {
                hideMonthAndDay()
            }

            TYPE_YEAR_MONTH -> {
                hideDay()
            }
        }
    }

    private fun initializeView() {
        confirm.setOnClickListener {
            val year: Int = datePicker.year
            val month: Int = datePicker.month + 1
            val day: Int = datePicker.dayOfMonth
            val monthFormat = if (month < 10) "0$month" else month.toString()
            val dayFormat = if (day < 10) "0$day" else day.toString()
            val dateFormat = when (pickType) {
                TYPE_ONLY_YEAR -> {
                    "$year"
                }

                TYPE_YEAR_MONTH -> {
                    "$year-$monthFormat-$dayFormat"
                }

                else -> {
                    "$year-$monthFormat-$dayFormat"
                }
            }

            onItemClick.onClick(dateFormat, pickTag)
            pickType = 0
            dismiss()
        }
    }

    fun setPickType(pickType: Int = TYPE_YEAR_MONTH_DAY, pickTag: String) {
        this.pickType = pickType
        this.pickTag = pickTag
    }


    @SuppressLint("DiscouragedApi")
    private fun hideMonthAndDay() {
        try {
            val monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
            if (monthSpinnerId != 0) {
                val monthSpinner: View? = datePicker.findViewById(monthSpinnerId)
                monthSpinner?.visibility = View.GONE
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }

        try {
            val daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
            if (daySpinnerId != 0) {
                val daySpinner: View? = datePicker.findViewById(daySpinnerId)
                daySpinner?.visibility = View.GONE
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    @SuppressLint("DiscouragedApi")
    private fun hideYear() {
        try {
            val yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
            if (yearSpinnerId != 0) {
                val yearSpinner: View? = datePicker.findViewById(yearSpinnerId)
                yearSpinner?.visibility = View.GONE
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    @SuppressLint("DiscouragedApi")
    private fun hideMonth() {
        try {
            val monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
            if (monthSpinnerId != 0) {
                val monthSpinner: View? = datePicker.findViewById(monthSpinnerId)
                monthSpinner?.visibility = View.GONE
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    @SuppressLint("DiscouragedApi")
    private fun hideDay() {
        try {
            val daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
            if (daySpinnerId != 0) {
                val daySpinner: View? = datePicker.findViewById(daySpinnerId)
                daySpinner?.setVisibility(View.GONE)
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }
}